# -*- coding: utf-8 -*-

from typing import List


java_utils_dict = {
    'Objects': 'java.util.Objects',
    'Date': 'java.util.Date',
    'List': 'java.util.List',
    'ArrayList': 'java.util.ArrayList',
    'Arrays': 'java.util.Arrays',
    'Map': 'java.util.Map',
    'HashMap': 'java.util.HashMap',
    'Maps': 'com.google.common.collect.Maps',
    'Collections': 'java.util.Collections',
    'Optional': 'java.util.Optional',
    'Collectors': 'java.util.stream.Collectors',
    'Lists': 'com.google.common.collect.Lists',
    'StringUtils': 'org.apache.commons.lang3.StringUtils',
    'CollectionUtils': 'org.apache.commons.collections.CollectionUtils',
    'BigDecimal': 'java.math.BigDecimal',
    'DecimalFormat': 'java.text.DecimalFormat',
    'MessageFormat': 'java.text.MessageFormat',
    'JSON': 'com.alibaba.fastjson.JSON',
    'Autowired': 'org.springframework.beans.factory.annotation.Autowired',
    'StreamObserver': 'io.grpc.stub.StreamObserver',
    'Slf4j': 'lombok.extern.slf4j.Slf4j',
    'GrpcEmpty': 'com.google.protobuf.Empty',
    'GrpcService': 'com.maycur.common.grpc.GrpcService'
}

grpc_java_type_dict = {
    'string': 'String',
    'bool': 'boolean',
    'int32': 'Integer',
    'int64': 'Long'
}


class JavaMethodLine(object):
    line: str
    n_indents: int

    def __init__(self, line: str, n_indents: int):
        self.line = line
        self.n_indents = n_indents


class JavaMethodBuilder(object):
    lines: List[JavaMethodLine]
    indents: str

    def __init__(self, indent_blanks: int):
        """
        init JavaMethodBuilder
        :param indent_blanks: 2 blanks or 4 blanks, or other not recommended one
        """
        self.lines = []

        if indent_blanks < 0:
            raise RuntimeError('invalid indent_blanks, cannot be negative')
        self.indents = ' ' * indent_blanks

    def add_method_line(self, line: JavaMethodLine):
        self.lines.append(line)

    def add_line(self, line: str, n_indents: int):
        self.lines.append(JavaMethodLine(line, n_indents))

    def build(self) -> str:
        res = ''
        for item in self.lines:
            res += self.indents * item.n_indents + item.line
            if '\n' not in item.line:
                res += '\n'
        return res
